package com.wm.blog_admin.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.blog_admin.dao.TTodoDAO;
import com.wm.blog_admin.query.TTodoQuery;
import com.wm.blog_common.base.BeanUtils;
import com.wm.blog_common.bean.PageHelper;
import com.wm.blog_common.bean.QueryPage;
import com.wm.blog_common.domain.TTodoDO;
import com.wm.blog_common.entity.TTodo;
import com.wm.blog_common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 代办事项表 服务实现类
 * </p>
 *
 * @author wm_yu
 * @since 2020-03-24
 */
@Service
public class TTodoService{
    @Autowired
    private TTodoDAO tTodoDAO;

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public Result<TTodoDO> selectById(Long id){
        return Result.success(BeanUtils.copyProperties(tTodoDAO.getById(id),TTodoDO.class));
    }

    /**
    * 根据ID删除
    * @param id
    * @return
    */
    public Result deleteById(Long id){
        return tTodoDAO.removeById(id)?Result.success():Result.failure();
    }

    /**
    * 添加
    * @param tTodo
    * @return
    */
    public Result insert(TTodo tTodo){
        return tTodoDAO.save(tTodo)?Result.success():Result.failure();
    }

    /**
    * 修改
    * @param tTodo
    * @return
    */
    public Result update(TTodo tTodo){
        return tTodoDAO.updateById(tTodo)?Result.success():Result.failure();
    }

    /**
    * 统计总数
    *
    * @param query
    * @return
    */
    public Result<Integer> count(TTodoQuery query) {
        return Result.success(tTodoDAO.count(this.combineQuery(query)));
    }

    /**
    * 列表查询
    * @param query
    * @return
    */
    public Result<List<TTodoDO>> list(TTodoQuery query){
        return Result.success(BeanUtils.copyListProperties(tTodoDAO.list(this.combineQuery(query)),TTodoDO.class));
    }
    /**
    * 分页查询
    * @param page
    * @return
    */
    public Result<Page<TTodoDO>> page(QueryPage page, TTodoQuery query){
        return Result.success(PageHelper.newPage(tTodoDAO.page(page,this.combineQuery(query)),TTodoDO.class));
    }

    /**
    * 组装查询条件
    *
    * @param query
    * @return
    */
    private QueryWrapper<TTodo> combineQuery(TTodoQuery query) {
        QueryWrapper<TTodo> wrapper = new QueryWrapper<>();
        //todo 查询条件 wrapper.eq("字段名","值")
        return wrapper;
    }

}







